package com.suneee.cloud.workbench.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suneee.base.util.ContextHelper;
import com.suneee.base.util.StringUtil;
import com.suneee.cloud.workbench.entity.Workbench;
import com.suneee.cloud.workbench.service.ConsoleService;
import com.suneee.enterprise.dao.EnterpriseDao;
import com.suneee.enterprise.entity.Enterprise;
import com.suneee.refund.dao.RefundApplyDao;
import com.suneee.refund.dao.ViewRefundApplyDao;
import com.suneee.refund.entity.RefundApply;
import com.suneee.refund.params.RefundParams;
import com.suneee.role.dao.RoleDao;
import com.suneee.role.entity.Role;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;
import com.suneee.sso.admin.entity.Administrator;
import com.suneee.store.bean.CompareRecordBean;
import com.suneee.store.dao.SkuDao;
import com.suneee.store.dao.SpuDao;
import com.suneee.store.entity.Sku;
import com.suneee.store.entity.Spu;
import com.suneee.store.service.ShopStockService;
import com.suneee.trades.dao.TradesInfoDao;
import com.suneee.trades.entity.TradesInfo;
import com.suneee.trades.resource.ShoppStatus;
import com.suneee.trades.resource.TradesStatus;

@Service("api-console")
public class ConsoleServiceImpl implements ConsoleService{

	@Autowired
	private EnterpriseDao enterpriseDao;
	
	@Autowired
	private SpuDao spuDao;
	
	@Autowired
	private SkuDao skuDao;
	
	@Autowired
	private TradesInfoDao tradesInfoDao;
	
	@Autowired
	private ShopDao shopDao;
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private ShopStockService shopStockService;
	
	@Autowired
	private ViewRefundApplyDao viewRefundApplyDao;
	
	@Autowired
	private RefundApplyDao refundApplyDao;
	
	
	
	@Override
	public Workbench platformConsole() {
		Workbench workbench = new Workbench();
		
		List<Enterprise> enterpriseList = enterpriseDao.select();
		if(enterpriseList != null)
			workbench.setEnterpriseNum(enterpriseList.size());
		
		List<Spu>  spuList =  spuDao.select();
		int spuNum = 0;
		if(spuList != null){
			for (Spu spu : spuList) {
				List<Sku> skuList = skuDao.selectWhere(" spu_id=?", spu.getSpuId());
				if(skuList != null)
					spuNum +=skuList.size();
			}
			workbench.setProductNum(spuNum);
		}
		
		List<TradesInfo> tradesInfoList = tradesInfoDao.select();
		if(tradesInfoList != null)
			workbench.setOrderNum(tradesInfoList.size());
		
		List<TradesInfo> finishedList = tradesInfoDao.selectWhere(" status=?", TradesStatus.TRADE_FINISHED);
		if(finishedList != null)
			workbench.setTradingVolumeNum(finishedList.size());
		
		List<Shop> shopList = shopDao.selectWhere(" status=?", Shop.AUDIT);
		if(shopList != null)
			workbench.setButtJoinNum(shopList.size());
		
		return workbench;
	}

	@Override
	public Workbench enterpriseConsole() {
		Workbench workbench = new Workbench();
		String roleName = "";
		Administrator admin = ContextHelper.getCurrentLoginInfo();
		
		workbench.setLogo(admin.getEnterprise().getLogo());
		workbench.setEnterpriseName(admin.getEnterprise().getName());
		workbench.setPackageName("");//暂无
		workbench.setValidityDate(null);//暂无
		workbench.setName(admin.getAccount());
		
		List<Role> roleList = roleDao.adminRoleList(admin.getAdminId(),ContextHelper.getCurrentLoginInfo().getPlatformFlag());
		if(roleList != null && roleList.size() >0){
			roleName = StringUtil.ObjectlistToString(roleList, ",");
		}
		workbench.setRoleName(roleName);//账号角色
		workbench.setLastLoginDate(null);//暂无
		
		//店铺数量
		List<Shop> shopList = shopDao.selectWhere(" shop_id IN (SELECT shop_id FROM t_shop_admin WHERE admin_id = ?) ", admin.getAdminId());
		if(shopList != null)
			workbench.setShopNum(shopList.size());
		
		//商品数量
		List<Spu> spuList = spuDao.selectWhere(" enterprise_code=?", admin.getEnterprise().getEnterpriseCode());
		int skuNum = 0;
		if(spuList != null){
			for (Spu spu : spuList) {
				List<Sku> skuList = skuDao.selectWhere(" spu_id=?", spu.getSpuId());
				if(skuList != null)
					skuNum +=skuList.size();
			}
		}
		workbench.setSpuNum(skuNum);
		
		//订单数量
		List<TradesInfo> orderList = tradesInfoDao.selectWhere(" enterprise_code=?", admin.getEnterprise().getEnterpriseCode());
		if(orderList != null)
			workbench.setTradesNum(orderList.size());
		
		//成交金额
		//"select sum(total_fee) from t_trades_info"
		List<Map<String, Object>> freeList = tradesInfoDao.getTotalFree(admin.getEnterprise().getEnterpriseCode());
		if(freeList != null){
			Map<String, Object> map = freeList.get(0);
			if(map.get("totalFee") != null){
				String totalFree = map.get("totalFee").toString();
				workbench.setTransactionAmount(Double.valueOf(totalFree));
			}
		}
		
		//店铺待对接
		List<Shop> shopInList = shopDao.toBeButtShopList(admin.getEnterprise().getEnterpriseCode());
		if(shopInList != null) 
			workbench.setToBeButtShop(shopInList.size());
		
		//待比对商品
		List<CompareRecordBean> stockList=shopStockService.getCompareRecordList();
		if(stockList != null)
			workbench.setCompairsNum(stockList.size());
		
		//待处理订单 审单
		List<TradesInfo> dealWithOrderList = tradesInfoDao.getDoInTrade(admin.getEnterprise().getEnterpriseCode());
		if(dealWithOrderList != null)
			workbench.setDealWithOrder(dealWithOrderList.size());
		
		//待发货订单 打单发货
		List<Map<String,Object>> tradeList = tradesInfoDao.queryForList("select * from v_shopp where "
				+" status in ('"+ShoppStatus.FAIL_SEND_GOODS.name()+"','"+ShoppStatus.WAIT_SEND_GOODS.name()+"') and enterprise_code=? "
				+ " and tid NOT IN (SELECT DISTINCT a.tid FROM t_trades_info a INNER JOIN t_refund_apply b ON a.tid = b.tid  WHERE a.enterprise_code = '"+ admin.getEnterprise().getEnterpriseCode() +"' AND (b.process_status = 'INIT' OR b.process_status = 'PROCESS')) "
				+" and shop_id in (select shop_id from t_shop_admin where admin_id = "+ContextHelper.getCurrentLoginInfo().getAdminId() +")"
				,admin.getEnterprise().getEnterpriseCode());
		if(tradeList != null)
		{
			workbench.setToSendOrderNum(tradeList.size());
		}
		RefundApply refundApply = new RefundApply();
		refundApply.setEnterpriseCode(admin.getEnterpriseCode());
		refundApply.setProcessStatus(RefundParams.PROCESS_STATUS_INIT);//待处理
		
		//订单待退款
		refundApply.setAfterSaleType(RefundParams.AFTER_SALE_TYPE_REFUND_MONEY);//退款
		//refundApply.setRefundResult(RefundParams.PROCESS_STATUS_INIT);//财务处理结果 INIT:未处理
		List<Map<String, Object>> refundRefundMoneyList = refundApplyDao.ConsoleRefundApply(refundApply);
		if(refundRefundMoneyList != null)
			workbench.setToReimburse(refundRefundMoneyList.size());
 
		//订单待退货
		refundApply.setAfterSaleType(RefundParams.AFTER_SALE_TYPE_REFUND_REFUND_GOODS);//退货
		//refundApply.setRefundResult("");//财务处理结果  这里不设置
		List<Map<String, Object>> refundRefundGoodsList = refundApplyDao.ConsoleRefundApply(refundApply);
		if(refundRefundGoodsList != null)
			workbench.setToReturnSales(refundRefundGoodsList.size());
		
		//订单待换货出库
		refundApply.setAfterSaleType(RefundParams.AFTER_SALE_TYPE_REFUND_CHANGE_GOODS);//换货
		refundApply.setProcessStatus(RefundParams.PROCESS_STATUS_PROCESS);
		List<Map<String, Object>> changeGoodsList = refundApplyDao.ConsoleRefundApply(refundApply);
		if(changeGoodsList != null)
			workbench.setToExchange(changeGoodsList.size());

		//订单待结束退货
		//refundApply.setProcessStatus("CLOSED");//待处理
		refundApply.setAfterSaleType(RefundParams.AFTER_SALE_TYPE_REFUND_REFUND_GOODS);
		List<Object> feedBackList = new ArrayList<Object>();
		//feedBackList.add(RefundParams.FEEDBACK_STATUS_ACCEPT);
		feedBackList.add(RefundParams.FEEDBACK_STATUS_DENY);
		refundApply.setFeedBackList(feedBackList);
		List<Map<String, Object>> toEndOfSalesList = refundApplyDao.ConsoleRefundApply(refundApply);
		if(toEndOfSalesList != null)
			workbench.setToEndOfSales(toEndOfSalesList.size());
		
		//订单待通知财务退款
		refundApply.setProcessStatus(RefundParams.PROCESS_STATUS_PROCESS);	
		refundApply.setAfterSaleType(null);//退款
		refundApply.setRefundResult(RefundParams.REFUND_RESULT_INIT);
		refundApply.setIsNoticeRefund("1");
		List<Map<String, Object>> toFinancialRefundList = refundApplyDao.ConsoleRefundApply(refundApply);
		if(toFinancialRefundList != null)
			workbench.setToFinancialRefund(toFinancialRefundList.size());
		
		return workbench;
	}

}
